import Mock from "mockjs";

// 生成模拟数据
const table = Mock.mock({
  "list|30": [
    {
      "id|+1": 1,
      name: "@cname",
      age: "@integer(20, 50)",
      "select|1-2": 2,
      "selectSearch|1-2": 2,
      "tree|1-2": 2,
      date: "@date",
      image: Mock.Random.image("200x200", "#4A7BF7", "Avatar"),
    },
  ],
}).list;

const list = Mock.mock({
  "list|2": [
    {
      "id|+1": 1,
      name: "@cname",
      age: "@integer(20, 50)",
      "select|1-2": 2,
      "selectSearch|1-2": 2,
      "tree|1-2": 2,
      date: "@date",
      image: Mock.Random.image("200x200", "#4A7BF7", "Avatar"),
    },
  ],
}).list;

// 使用 Mock.js 模拟数据
Mock.mock("/api/user/page", "get", ({ query = {} }) => {
  // 解析查询参数
  const {
    current = 1,
    size = 10,
    name,
    age,
    select,
    selectSearch,
    tree,
    date,
  } = query;

  let data = [...table];

  // 过滤逻辑
  if (name) data = data.filter((item) => item.name.includes(name.toString()));
  if (age) data = data.filter((item) => item.age === parseInt(age));
  if (select) data = data.filter((item) => item.select === parseInt(select));
  if (selectSearch)
    data = data.filter((item) => item.selectSearch === parseInt(selectSearch));
  if (tree) data = data.filter((item) => item.tree === parseInt(tree));
  if (date) data = data.filter((item) => item.date === date);

  // 分页处理
  const pageSize = parseInt(size);
  const pageNum = parseInt(current);
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;

  return {
    code: 200,
    data: data.slice(start, end),
    total: data.length,
    current: pageNum,
    size: pageSize,
  };
});

// 使用 Mock.js 模拟数据
Mock.mock("/api/user/list", "get", ({ query = {} }) => {
  return {
    code: 200,
    data: list,
  };
});
